package com.frame.work.common.util;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author wjming
 */
public class Time {
    /**
     * 获取当前时间单位(秒)
     *
     * @return
     */
    public static Long NowTimeToSecond() {
        Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        return second;
    }

    /**
     * 获取当前时间单位(毫秒)
     *
     * @return
     */
    public static Long NowTimeToMilliSecond() {
        Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return milliSecond;
    }

    /**
     * 时间转化为时间戳毫秒
     */
    public static Long toLong(LocalDateTime localDateTime) {
        if (localDateTime != null) {
            return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        } else {
            return null;
        }
    }

    public static String stampToDate(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time_Date = sdf.format(new Date(time));
        return time_Date;

    }

    /**
     * 获取当前年月
     *
     * @return
     */
    public static String getStringNowYearAndMonth() {
        LocalDateTime localDateTime = LocalDateTime.now();
        int year = localDateTime.getYear();
        int month = localDateTime.getMonth().getValue();
        if (month < 10) {
            return year + "_0" + month;
        } else {
            return year + "_" + month;
        }
    }
}
